<?php
function retrieveData($app=NULL, $post_params){
	switch($app){
		case "slideshare":
			return slideshare($post_params);
			break;
		case "flickr":
			return flickr($post_params);
			break;
		default:
	}
}

function flickr( $post_params ){

	$params = array();
	
	$params['api_key'] = 'chiave_api';
	$params['method'] = 'flickr.photos.search';
	$params['format'] = 'rest';
	$params['text'] = $post_params['q'];
	
	# numero di foto per pagina. di default sono 100
	# il massimo è 500	
	$params['per_page'] = $post_params['items_per_page'] ? $post_params['items_per_page'] : "20";
	
	# numero di pagina visualizzata	
	$params['page'] = $post_params['page'] ? $post_params['page'] : "1";
	$params['extras'] = 'owner_name,date_upload';

	# stabilisce l'ordinamento delle fotografie	# di dafault è date-posted-desc	# possibili valori sono date-posted-asc, 	# date-posted-desc, date-taken-asc, date-taken-desc, 	# interestingness-desc, interestingness-asc, e relevance	
		$params['sort'] = 'relevance'; 
	
	$encoded_params = array();

	foreach ($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}

	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);

	$ch = curl_init();    
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_VERBOSE, 1); 
	curl_setopt($ch, CURLOPT_URL, $url); 
	$result = curl_exec($ch); 
	curl_close($ch); 
	
	return $result;
}

function slideshare( $params ){

	$api_method = "http://www.slideshare.net/api/2/search_slideshows?";
	$secret = "secret";
	$ts = time();
	
	$params['api_key'] = "chiave_api";
	$params['ts'] = $ts; 
	$params['hash'] = sha1($secret . $ts);
	$params['items_per_page'] = "12";
	

	$encoded_params = array();
	foreach ($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	$url = $api_method . implode('&', $encoded_params);

	$ch = curl_init();    
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $url); 
	$result = curl_exec($ch); 
	curl_close($ch);
	return $result; 
}